package com.yyj.web.controller;

import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.constant.WebConstant;
import com.yyj.model.TPropertyOwner;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.UserFormService;
import com.yyj.service.UserService;
import com.yyj.util.RspUtils;
import com.yyj.web.protocol.req.*;
import com.yyj.web.protocol.resp.UserInfoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "业主相关接口" , description="业主相关接口")
@RestController
public class OwnerController extends BaseController{

    @Autowired
    UserService userService;

    @Autowired
    UserFormService userFormService;

    @ApiOperation(value = "业主注册")
    @RequestMapping(value = "/owners/sign" , method = RequestMethod.POST)
    public BaseResponse userSign(@RequestBody @Validated UserSignReq userSignReq , BindingResult result){
        try{
            checkRequestParams(result);
            UserInfoResp ownerInfoResp = userService.userSign(userSignReq);
            SessionInfo sessionInfo = new SessionInfo();
            sessionInfo.setMobile(ownerInfoResp.getMobile());
            session.setAttribute(WebConstant.SESSION_INFO, sessionInfo);
            return RspUtils.success(ownerInfoResp , "注册成功！");

        }catch(Exception e){
            logger.error("用户注册异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }

    }

    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/users/login" , method = RequestMethod.POST)
    public BaseResponse userLogin(@RequestBody @Validated UserLoginReq userLoginReq , BindingResult result){
        try{
            checkRequestParams(result);
            UserInfoResp userInfoResp = userService.userLogin(userLoginReq);
            SessionInfo sessionInfo = new SessionInfo();
            sessionInfo.setMobile(userInfoResp.getMobile());
            sessionInfo.setRoomNum(userInfoResp.getRoomNum());
            sessionInfo.setAdmin(userInfoResp.isAdmin());
            sessionInfo.setIsRoot(userInfoResp.getIsRoot());
            sessionInfo.setOpenId(userInfoResp.getOpenId());
            session.setAttribute(WebConstant.SESSION_INFO, sessionInfo);
            return RspUtils.success(userInfoResp , "登录成功！");

        }catch(Exception e){
            logger.error("用户登录异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }

    }

    @ApiOperation(value = "业主设置房号")
    @RequestMapping(value = "/owners/initRoomNum" , method = RequestMethod.POST)
    public BaseResponse initRoomNum(@RequestBody @Validated UserInitRoomReq userInitRoomReq , BindingResult result){

        try{
            checkRequestParams(result);
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            userInitRoomReq.setMobile(sessionInfo.getMobile());
            UserInfoResp ownerInfoResp = userService.initRoomNum(userInitRoomReq);
            sessionInfo.setRoomNum(ownerInfoResp.getRoomNum());
            session.setAttribute(WebConstant.SESSION_INFO, sessionInfo);
            return RspUtils.success("" , "设置房号成功！");

        }catch(Exception e){
            logger.error("设置房号异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }

    }

    @ApiOperation(value = "用户授权登录")
    @RequestMapping(value = "/users/auth" , method = RequestMethod.POST)
    public BaseResponse userLogin(
            @RequestBody @Validated WeixinAuthReq weixinAuthReq , HttpServletRequest request , HttpSession session, BindingResult result
    ){
        try{
            checkRequestParams(result);
            UserInfoResp userInfoResp = userService.initOwnerByAuth(weixinAuthReq);
            SessionInfo sessionInfo = new SessionInfo();
            sessionInfo.setOpenId(userInfoResp.getOpenId());
            sessionInfo.setMobile(userInfoResp.getMobile());
            sessionInfo.setAdmin(userInfoResp.isAdmin());
            sessionInfo.setRoomNum(userInfoResp.getRoomNum());
            session.setAttribute(WebConstant.SESSION_INFO, sessionInfo);
            return RspUtils.success(userInfoResp);
        }catch(Exception e){
            logger.error("用户授权登录异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/owners/testAuth" , method = RequestMethod.POST)
    public BaseResponse userLogin(
            String mobile , String openId ,  HttpSession session
    ){
        try{
            UserInfoResp userInfoResp = userService.testAuth(mobile , openId);
            SessionInfo sessionInfo = new SessionInfo();
            sessionInfo.setOpenId(userInfoResp.getOpenId());
            sessionInfo.setMobile(userInfoResp.getMobile());
            sessionInfo.setAdmin(userInfoResp.isAdmin());
            sessionInfo.setRoomNum(userInfoResp.getRoomNum());
            session.setAttribute(WebConstant.SESSION_INFO, sessionInfo);
            return RspUtils.success(userInfoResp);
        }catch(Exception e){
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主信息查询")
    @RequestMapping(value = "/auth/owners/info" , method = RequestMethod.GET)
    public BaseResponse userInfo(){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            return RspUtils.success(sessionInfo);
        }catch(Exception e){
            logger.error("业主信息查询异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "修改业主信息")
    @RequestMapping(value = "/auth/owners/update" , method = RequestMethod.POST)
    public BaseResponse updateOwner(@RequestBody @Validated OwnerUpdateReq ownerUpdateReq){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            userService.updateOwner(sessionInfo.getMobile() , ownerUpdateReq);
            return RspUtils.success("修改成功");
        }catch(Exception e){
            logger.error("业主信息修改异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "采集业主formId")
    @RequestMapping(value = "/auth/owners/setFormId" , method = RequestMethod.POST)
    public BaseResponse setOwnerFormId(@RequestBody OwnerFormIdReq ownerFormIdReq){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            userFormService.setUserFormId(sessionInfo.getOpenId(),  ownerFormIdReq.getFormId());
            return RspUtils.success("成功");
        }catch(Exception e){
            logger.error("采集业主formId异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

}
